package com.hong.qrcodeutil;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsBuildBitmapOption;

public class ScannerActivity extends AppCompatActivity {

    private ImageView ivQrcode;
    private Button btnCreate;
    private EditText etContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_build_bitmap);
        ivQrcode = findViewById(R.id.iv_qrcode);
        etContent = findViewById(R.id.et_content);
        btnCreate = findViewById(R.id.btn_create);
        btnCreate.setOnClickListener(view -> {
            String content = etContent.getText().toString().trim();
            if (content.isEmpty()) {
                Toast.makeText(this, "请输入生成二维码的内容", Toast.LENGTH_SHORT).show();
            } else {
                Bitmap bitmap = null;
                try {
                    bitmap = ScanUtil.buildBitmap(content, 3, 300, 300, new HmsBuildBitmapOption.Creator().create());
                    ivQrcode.setImageBitmap(bitmap);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(this, "生成二维码失败", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}